
-- 火焰弹类
Fireball = {}
Fireball.__index = Fireball
function Fireball:new(x, y, targetx,targety, side , speed, impactDamage)
    local fireball = {
        x = x,
        y = y,
        targetx = targetx,
        targety = targety,
        side = side, -- 那个玩家的
        speed = speed,
        impactDamage = impactDamage,
        alive = true,   --是否活着
        -- lifetime = 3  -- 火焰弹存在时间
    }
    setmetatable(fireball, Fireball)
    return fireball
end

function Fireball:update(dt,fireAreas)
    -- self.lifetime = self.lifetime - dt
    if self.alive then
        local dx = self.targetx - self.x
        local dy = self.targety - self.y
        local distance = math.sqrt(dx * dx + dy * dy)
        if distance < 5 then
            self.alive = false
            self.x =self.targetx
            self.y =self.targety 
            -- -- 生成火焰区域
            -- local fireArea = FireArea:new(self.x, self.y, 2, 5)  -- 每秒造成2点伤害
            -- table.insert(fireAreas, fireArea)
        else
            self.x = self.x + (dx / distance) * self.speed * dt
            self.y = self.y + (dy / distance) * self.speed * dt
        end

    end
    
end

function Fireball:isAlive()
    return self.alive 
end

function Fireball:explode(enemies,fireAreas)
    -- 造成爆炸伤害（可选）
    -- for _, enemy in ipairs(enemies) do
    --     local distance = math.sqrt((enemy.x - self.x)^2 + (enemy.y - self.y)^2)
    --     if distance < 50 then  -- 设定爆炸范围
    --         enemy.health = enemy.health - self.impactDamage
    --         print("火焰弹爆炸，敌人剩余生命：" .. enemy.health)
    --     end
    -- end

    -- 生成火焰区域
    -- local fireArea = FireArea:new(self.x, self.y, 2, 5)  -- 每秒造成2点伤害
    local fireArea = FireArea:new(self.targetx, self.targety, self.side, 2, 5)  -- 每秒造成2点伤害
    table.insert(fireAreas, fireArea)
end
